<?php
/**
 * Created by PhpStorm.
 * User: ZenGo
 * Date: 2017/2/9
 * Time: 23:50
 * 简单工厂设计模式
 */
namespace DesignPattern\FactorPattern;

interface Shape
{
    public function draw();
}

class Rectangle implements Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Square::draw() method.");
    }
}

class Circle implements Shape
{
    public function draw()
    {
        // TODO: Implement draw() method.
        print_r("Inside Circle::draw() method.");
    }
}

class ShapeFactory
{
    public function getShape($shapeType)
    {
        if ($shapeType == null) {
            return null;
        }

        $type = strtoupper($shapeType);
        if ($type == "CIRCLE") {
            return new Circle();
        } else if ($type == "RECTANGLE") {
            return new Rectangle();
        } else if ($type == "SQUARE") {
            return new Square();
        }
        return null;

    }
}

$type = 'RECTANGLE';
$shapeFac = new ShapeFactory();
if ($shapeFac->getShape($type)) {
    $shapeFac->getShape($type)->draw();
} else {
    print_r("类型不存在");
}